<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>How can I wrap a function which needs to take ownership of a raw pointer?</title>
<link rel="stylesheet" href="../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Python">
<link rel="up" href="../faq.html" title="Chapter 5. Frequently Asked Questions (FAQs)">
<link rel="prev" href="how_can_i_find_the_existing_pyob.html" title="How can I find the existing PyObject that holds a C++ object?">
<link rel="next" href="compilation_takes_too_much_time_.html" title="Compilation takes too much time and eats too much memory! What can I do to make it faster?">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../images/boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="how_can_i_find_the_existing_pyob.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../faq.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="compilation_takes_too_much_time_.html"><img src="../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="faq.how_can_i_wrap_a_function_which0"></a><a class="link" href="how_can_i_wrap_a_function_which0.html" title="How can I wrap a function which needs to take ownership of a raw pointer?">How can I wrap
      a function which needs to take ownership of a raw pointer?</a>
</h3></div></div></div>
<p>
        <span class="bold"><strong>Q:</strong></span> Part of an API that I'm wrapping goes
        something like this:
      </p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">A</span> <span class="special">{};</span> <span class="keyword">struct</span> <span class="identifier">B</span> <span class="special">{</span> <span class="keyword">void</span> <span class="identifier">add</span><span class="special">(</span> <span class="identifier">A</span><span class="special">*</span> <span class="special">);</span> <span class="special">}</span>
<span class="identifier">where</span> <span class="identifier">B</span><span class="special">::</span><span class="identifier">add</span><span class="special">()</span> <span class="identifier">takes</span> <span class="identifier">ownership</span> <span class="identifier">of</span> <span class="identifier">the</span> <span class="identifier">pointer</span> <span class="identifier">passed</span> <span class="identifier">to</span> <span class="identifier">it</span><span class="special">.</span>
</pre>
<p>
        However:
      </p>
<pre class="programlisting"><span class="identifier">a</span> <span class="special">=</span> <span class="identifier">mod</span><span class="special">.</span><span class="identifier">A</span><span class="special">()</span>
<span class="identifier">b</span> <span class="special">=</span> <span class="identifier">mod</span><span class="special">.</span><span class="identifier">B</span><span class="special">()</span>
<span class="identifier">b</span><span class="special">.</span><span class="identifier">add</span><span class="special">(</span> <span class="identifier">a</span> <span class="special">)</span>
<span class="identifier">del</span> <span class="identifier">a</span>
<span class="identifier">del</span> <span class="identifier">b</span>
<span class="preprocessor"># python</span> <span class="identifier">interpreter</span> <span class="identifier">crashes</span>
<span class="preprocessor"># later</span> <span class="identifier">due</span> <span class="identifier">to</span> <span class="identifier">memory</span> <span class="identifier">corruption</span><span class="special">.</span>
</pre>
<p>
        Even binding the lifetime of a to b via <code class="computeroutput"><span class="identifier">with_custodian_and_ward</span></code>
        doesn't prevent the python object a from ultimately trying to delete the
        object it's pointing to. Is there a way to accomplish a 'transfer-of-ownership'
        of a wrapped C++ object?
      </p>
<p>
        --Bruce Lowery
      </p>
<p>
        Yes: Make sure the C++ object is held by auto_ptr:
      </p>
<pre class="programlisting"><span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">auto_ptr</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span> <span class="special">&gt;(</span><span class="string">"A"</span><span class="special">)</span>
  <span class="special">...</span>
  <span class="special">;</span>
</pre>
<p>
        Then make a thin wrapper function which takes an auto_ptr parameter:
      </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">b_insert</span><span class="special">(</span><span class="identifier">B</span> <span class="special">&amp;</span><span class="identifier">b</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">auto_ptr</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span> <span class="identifier">a</span><span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">b</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">a</span><span class="special">.</span><span class="identifier">get</span><span class="special">());</span>
  <span class="identifier">a</span><span class="special">.</span><span class="identifier">release</span><span class="special">();</span>
<span class="special">}</span>
</pre>
<p>
        Wrap that as B.add. Note that pointers returned via <code class="computeroutput"><span class="identifier">manage_new_object</span></code>
        will also be held by <code class="computeroutput"><span class="identifier">auto_ptr</span></code>,
        so this transfer-of-ownership will also work correctly.
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2015 David
      Abrahams, Stefan Seefeld<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="how_can_i_find_the_existing_pyob.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../faq.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="compilation_takes_too_much_time_.html"><img src="../images/next.png" alt="Next"></a>
</div>
</body>
</html>
